package com.photo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photo.auto.JSONAuthentication;
import com.photo.entity.PhPhoto;
import com.photo.entity.PhUser;
import com.photo.entity.PhotoPrice;
import com.photo.mapper.PhPhotoMapper;
import com.photo.mapper.PhotoPriceMapper;
import com.photo.response.Result;
import com.photo.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author photo
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/photo-price")
public class PhotoPriceController extends JSONAuthentication {
@Autowired
PhotoPriceMapper photoPriceMapper;
@Autowired
PhPhotoMapper phPhotoMapper;
    @ApiOperation(value = "向相册添加照片")
    @PostMapping("/add")
    public void addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("phId") int phId) throws Exception {
        PhotoPrice photo = new PhotoPrice();
        photo.setPhId(phId);
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        photo.setCreateTime(timestamp);
        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img", imageFileName);
        Path path = Paths.get("D:\\自己的项目\\订餐系统\\order\\src\\assets\\picture\\" + fileName);
        Files.write(path, bytes);//写入文件
        String avatar_url = fileName;
        photo.setPicture(avatar_url);
        response.reset();
            if (photoPriceMapper.insert(photo) > 0) {
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
    }

    @ApiOperation(value = "删除相册相片")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = photoPriceMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据相册id查询照片")
    @PostMapping("/findAllPhotoById")
    public void findAllPhotoById(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("phId") int phId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("ph_id",phId);
        Page<PhotoPrice> page = new Page<>(current, size);
        Page<PhotoPrice> pages = photoPriceMapper.selectPage(page, wrapper);
        List<PhotoPrice> list = pages.getRecords();
        for(PhotoPrice a:list){
            a.setPhoto(phPhotoMapper.selectById(a.getPhId()));
        }
        int total = phPhotoMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("user", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有照片");
        this.WriteJSON(request, response, error);
    }

}

